<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title>图书信息表</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

	<script src="../../scripts/boot.js" type="text/javascript"></script>
</head>

<body>
	<div style="width: 100%;">
		<div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
			<table style="width: 100%;">
				<tr>
					<td style="width: 100%;">
						<a class="mini-button" iconCls="icon-add" onclick="add()">增加</a>
						<a class="mini-button" iconCls="icon-no" onclick="delRow()" style="margin-left: 10px;">删除</a>
					</td>
					<td style="white-space: nowrap;">
						<input id="bookname" class="mini-textbox" emptyText="请输入图书名称" style="width:150px;"
							onenter="onKeyEnter" />
						<input id="booktype" class="mini-combobox" style="width: 150px;" showNullItem="true"
							emptyText="请输入图书类别" url="../../data/booktype.json" onenter="onKeyEnter" />
						<a class="mini-button" onclick="search()">查询</a>
						<a class="mini-button" onclick="clear()">清空</a>
					</td>
				</tr>
			</table>
		</div>
	</div>
	<div id="booklist" class="mini-datagrid" style="width: 100%; height: auto;" url="../../bookinfolistaction"
		idField="id" multiSelect="true">
		<div property="columns">
			<div type="checkcolumn"></div>
			<div field="bookId" width="120" headerAlign="center" allowSort="true">图书编号</div>
			<div field="bookName" width="120" headerAlign="center" allowSort="true">图书名称</div>
			<div field="publisher" width="100" headerAlign="center" allowSort="true">出版社</div>
			<div field="author" width="100" headerAlign="center" allowSort="true">作者</div>
			<div field="bookType" width="120" headerAlign="center" allowSort="true" renderer="onBookTypeRenderer">图书类别
			</div>
			<div field="remain" width="120" headerAlign="center" allowSort="true" renderer="onRemainRenderer">剩余数量</div>
			<div name="action" width="120" headerAlign="center" renderer="onActionRenderer" cellStyle="padding:0;">操作
			</div>

		</div>
	</div>


	<script type="text/javascript">
		mini.parse();

		var grid = mini.get("booklist");
		grid.load();

		/* 操作按钮 */
		function onActionRenderer(e) {
			var grid = e.sender;
			var record = e.record;
			var uid = record._id;
			var rowIndex = e.rowIndex;

			var s = ' <a class="mini-button" onclick="editRow()">编辑</a>'
				+ ' <a class="mini-button" onclick="delRow()">删除</a>';
			if (record.remain > 0) {
				s += ' <a class="mini-button" onclick="addBorrow()">借阅</a>';
			}
			s += ' <a class="mini-button" onclick="openDetail()">详情</a>';

			return s;
		}

		// 新增图书
		function add() {

			mini.open({
				url: "pages/bookinfo/bookinfoadd.html",
				title: "新增图书",
				width: 500,
				height: 350,
				allowResize: false,
				ondestroy: function (action) {

					grid.reload();
				}
			});
		}
		// 编辑图书
		function editRow() {
			var row = grid.getSelected();
			if (row) {
				mini.open({
					url: "pages/bookinfo/bookinfoedit.html?bookid="+row.bookId,
					title: "编辑图书信息",
					width: 500,
					height: 350,
					allowResize: false,
					onload: function () {
						var iframe = this.getIFrameEl();
						var data = {
							action: "edit",
							id: row.bookId
						};
						iframe.contentWindow.SetData(data);

					},
					ondestroy: function (action) {
						grid.reload();

					}
				});

			} else {
				alert("请选中一条记录");
			}
		}
		// 删除图书
		function delRow() {
			var rows = grid.getSelecteds();
			if (rows.length > 0) {
				mini.confirm("确定删除记录？", "确定？",
					function (action) {
						if (action == "ok") {
							var ids = [];
							for (var i = 0, l = rows.length; i < l; i++) {
								var r = rows[i];
								ids.push(r.bookId);
							}
							var id = ids.join(',');
							grid.loading("操作中，请稍后......");
							$.ajax({
								url: "../../bookinfodelaction",
								data: {
									ids: id
								},
								success: function (text) {
									var res = mini.decode(text);
									if(res.code == 1){
										showTips(res);
									}else{
										mini.alert(res.msg);
									}
									grid.reload();
								},
								error: function () {
								}
							});
						}
					}
				);
			} else {
				alert("请选中一条记录");
			}
		}
		// 借阅功能
		function addBorrow() {
			var row = grid.getSelected();
			mini.open({
				url: "pages/borrowinfo/borrowinfoadd.html",
				title: "新增借阅信息",
				width: 500,
				height: 350,
				allowResize: false,
				onload: function () {
					var iframe = this.getIFrameEl();
					var data = {
						id: row.bookId
					};
					iframe.contentWindow.SetData(data);
				},
				ondestroy: function (action) {
					grid.reload();
				}
			});
		}
		// 图书详情信息展示
		function openDetail() {
			var row = grid.getSelected();
			if (row) {
				mini.open({
					url: "pages/bookinfo/bookinfodetail.html",
					title: "图书详情信息",
					width: 500,
					height: 200,
					allowResize: false,
					onload: function () {
						var iframe = this.getIFrameEl();
						var data = {
							id: row.bookId
						};
						iframe.contentWindow.SetData(data);
					},
					ondestroy: function (action) {
						grid.reload();
					}
				});

			} else {
				alert("请选中一条记录");
			}
		}
		// 列表筛选
		function search() {
			var bookname = mini.get("bookname").getValue();
			var booktype = mini.get("booktype").getValue();
			grid.load({
				bookName: bookname,
				bookType: booktype
			});
		}
		// 清空筛选条件
		function clear(){
			mini.get("bookname").setValue("");
			mini.get("booktype").select(0);
			grid.load();
		}
		function onKeyEnter(e) {
			search();
		}

		// 判断图书类别
		var Genders = [{
			booktype: 1,
			text: '文学类'
		}, {
			booktype: 2,
			text: '科技类'
		}, {
			booktype: 3,
			text: '艺术类'
		}, {
			booktype: 4,
			text: '教育类'
		}, {
			booktype: 5,
			text: '其他'
		},];
		function onBookTypeRenderer(e) {
			for (var i = 0, l = Genders.length; i < l; i++) {
				var g = Genders[i];
				if (g.booktype == e.value)
					return g.text;
			}
			return "";
		}
		// 判断剩余数量
		function onRemainRenderer(e) {
			if (e.value <= 10) {
				e.cellStyle = "color:red;font-weight:bold";
			}
			return e.value;
		}
		// 信息提示
		function showTips(res) {
			if (res.code == 1) {
				mini.showTips({
					content: "<b>" + res.msg + "</b>",
					x: "center",
					y: "top",
					state: "success",
					timeout: 3000
				});
			} else {
				mini.showTips({
					content: "<b>" + res.msg + "</b>",
					x: "center",
					y: "top",
					state: "danger",
					timeout: 3000
				});
			}
		}
	</script>

</body>

</html>